#----------------------------------------------------------------------------------------------------#
# Created by: Drew Engelhardt 3-3-15
# Last modified by: Drew Engelhardt 3-18-15
# Filename: randomization inference
# File purpose: Investigating randomization inference
# NOTE TO RUN THIS AND HAVE RESULTS DISPLAY USE COMMAND LIKE: > source("/Users/adamseth/Desktop/randomization inference.R", echo=TRUE)
#----------------------------------------------------------------------------------------------------#
library(foreign)
library(ri)
setwd("~/Desktop/")

dat <- read.dta("ri_dataset_direct_mail.dta")
dat <- subset(dat, UNDELIVERED == 0)

# subsetting to compare each treatment individually with the control group
dat.ret <- subset(dat, PROSPECTIVE != 1)
dat.pro <- subset(dat, RETROSPECTIVE != 1)
dat.pro1 <- subset(dat, CONTROL != 1)
set.seed(110181)

#----------------------------------------------------------------------------------------------------#
# Binary Donation Measure
#----------------------------------------------------------------------------------------------------#

# Comparing retrospective with control
retro.perms <- genperms(dat.ret$RETROSPECTIVE) 
# probability of assignment to given treatment
retro.probs <- genprobexact(dat.ret$RETROSPECTIVE)
# Average Treatment Effect calculation for retrospective condition without covariate adjustment
retro.ate <- estate(dat.ret$donation01, dat.ret$RETROSPECTIVE, X = NULL, prob = retro.probs, HT = FALSE)
# Calculation of hypothetical potential outcomes with a 0 treatment effect
retro.outs <- genouts(dat.ret$donation01, dat.ret$RETROSPECTIVE)
# Randomization distribution of potential outcomes under ATE = 0
retro.dist <- gendist(retro.outs, retro.perms, retro.probs)
# Compares distribution of potential outcomes under ATE = 0 to estimated ATE for treatment and saving plot
pdf("retro_binary.pdf", height = 6.75, width = 8.25)
dispdist(retro.dist, retro.ate)
dev.off()


# Repeating above for prospective treatment (comparing with control)
pro.perms <- genperms(dat.pro$PROSPECTIVE)
pro.probs <- genprobexact(dat.pro$PROSPECTIVE)
pro.ate <- estate(dat.pro$donation01, dat.pro$PROSPECTIVE, X = NULL, prob = pro.probs, HT = FALSE)
pro.outs <- genouts(dat.pro$donation01, dat.pro$PROSPECTIVE)
pro.dist <- gendist(pro.outs, pro.perms, pro.probs)
pdf("pro_binary.pdf", height = 6.75, width = 8.25)
dispdist(pro.dist, pro.ate)
dev.off()

# Repeating above for prospective treatment (comparing with retrospective rather than control)
pro1.perms <- genperms(dat.pro1$PROSPECTIVE)
pro1.probs <- genprobexact(dat.pro1$PROSPECTIVE)
pro1.ate <- estate(dat.pro1$donation01, dat.pro1$PROSPECTIVE, X = NULL, prob = pro1.probs, HT = FALSE)
pro1.outs <- genouts(dat.pro1$donation01, dat.pro1$PROSPECTIVE)
pro1.dist <- gendist(pro1.outs, pro1.perms, pro1.probs)
pdf("pro1_binary.pdf", height = 6.75, width = 8.25)
dispdist(pro1.dist, pro1.ate)
dev.off()

#----------------------------------------------------------------------------------------------------#

#----------------------------------------------------------------------------------------------------#
# Continuous Donation Measure
#----------------------------------------------------------------------------------------------------#
# Same analysis with continuous donation measure as outcome
retro.perms.con <- genperms(dat.ret$RETROSPECTIVE)
retro.probs.con <- genprobexact(dat.ret$RETROSPECTIVE)
retro.ate.con <- estate(dat.ret$DONATION, dat.ret$RETROSPECTIVE, X = NULL, prob = retro.probs.con, HT = FALSE)
retro.outs.con <- genouts(dat.ret$DONATION, dat.ret$RETROSPECTIVE)
retro.dist.con <- gendist(retro.outs.con, retro.perms.con, retro.probs.con)
pdf("retro_cont.pdf", height = 6.75, width = 8.25)
dispdist(retro.dist.con, retro.ate.con)
dev.off()

# Repeating above for prospective treatment (comparing with control)
pro.perms.con <- genperms(dat.pro$PROSPECTIVE)
pro.probs.con <- genprobexact(dat.pro$PROSPECTIVE)
pro.ate.con <- estate(dat.pro$DONATION, dat.pro$PROSPECTIVE, X = NULL, prob = pro.probs.con, HT = FALSE)
pro.outs.con <- genouts(dat.pro$DONATION, dat.pro$PROSPECTIVE)
pro.dist.con <- gendist(pro.outs.con, pro.perms.con, pro.probs.con)
pdf("pro_cont.pdf", height = 6.75, width = 8.25)
dispdist(pro.dist.con, pro.ate.con)
dev.off()

# Repeating above for prospective treatment (comparing with retrospective rather than control)
pro1.perms.con <- genperms(dat.pro1$PROSPECTIVE)
pro1.probs.con <- genprobexact(dat.pro1$PROSPECTIVE)
pro1.ate.con <- estate(dat.pro1$DONATION, dat.pro1$PROSPECTIVE, X = NULL, prob = pro1.probs.con, HT = FALSE)
pro1.outs.con <- genouts(dat.pro1$DONATION, dat.pro1$PROSPECTIVE)
pro1.dist.con <- gendist(pro1.outs.con, pro1.perms.con, pro1.probs.con)
pdf("pro1_cont.pdf", height = 6.75, width = 8.25)
dispdist(pro1.dist.con, pro1.ate.con)
dev.off()
